import 'dart:async';

import 'package:check_app/api/request.dart';
import 'package:check_app/model/http_model.dart';
import 'package:check_app/model/taskInfo.dart';
import 'package:check_app/model/taskList.dart';
import 'package:check_app/util/pref_util.dart';

const EXTRA_PATH = '/app/task/submit';
const NORMAL_PATH = '/input/save';
const EDIT_NORMAL_PATH = '/app/task/normal/edit';

// 任务接口
class TaskDao {
  // 任务列表
  static Future fetchTaskList(path, params) async {
    final response = await Request().get(path, queryParameters: params);
    return TaskListResult.fromJson(response);
  }

  // 任务详情
  static Future fetchTaskInfo(path) async {
    final response = await Request().get(path, queryParameters: {});
    return TaskInfoResult.fromJson(response);
  }

  // 任务详情全部
  static Future fetchTaskInfoFull(path) async {
    final response = await Request().get(path, queryParameters: {});
    if (response['success']) {
      await PrefUtil().setTaskInfo(response['data']);
    }
    return TaskInfoResult.fromJson(response);
  }

  static Future submit(params) async {
    final response = await Request().get(EXTRA_PATH, queryParameters: params);
    return HttpModelResult.fromJson(response);
  }

  static Future save(params) async {
    final response = await Request().post(NORMAL_PATH, queryParameters: params);
    return HttpModelResult.fromJson(response);
  }

  static Future editNormalTask(params) async {
    final response =
        await Request().post(EDIT_NORMAL_PATH, queryParameters: params);
    return HttpModelResult.fromJson(response);
  }
}
